﻿using GrainManage.Entities.Trade;
using GrainManage.Proxy;
using GrainManage.Web.Models;
using GrainManage.Web.Models.Trade;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GrainManage.Web.Controllers
{
    public class TradeController : BaseController
    {
        public ActionResult SearchTrade()
        {
            return View();
        }

        public ActionResult NewTrade(int contactID)
        {
            var model = new AddTradeModel
            {
                ContactID = contactID
            };
            return View(model);
        }

        public ActionResult EditTrade(int tradeID)
        {
            var inputPara = new InputGetByID
            {
                Authentication = CurrentAccount,
                TradeID = tradeID
            };
            var tradeProxy = ObjectFactory.TradeProxy;
            var result = tradeProxy.GetByID(inputPara);
            var trade = result.Trade;
            var model = new UpdateTradeModel
            {
                TradeID = trade.TradeID,
                ActualMoney = trade.ActualMoney,
                ContactID = trade.ContactID,
                Grain = trade.Grain,
                Price = trade.Price,
                Remarks = trade.Remarks,
                TradeType = trade.TradeType,
                Weight = trade.Weight

            };
            return View(model);
        }

        [HttpPost]
        public ActionResult GetByID(int tradeID)
        {
            var inputPara = new InputGetByID
            {
                Authentication = CurrentAccount,
                TradeID = tradeID
            };
            var tradeProxy = ObjectFactory.TradeProxy;
            var result = tradeProxy.GetByID(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return View(Constant.RestStatusView, result);
        }

        [HttpPost]
        public ActionResult GetDetail(TradeContactAreaFilterModel input, PageModel page)
        {
            var inputPara = new InputSearchDetail
            {
                Authentication = CurrentAccount,
                PageIndex = page.pageIndex,
                PageSize = page.pageSize,
                TradeFilter = new Data.Model.Filter.TradeTotalWithContactFilter
                {
                    Area = Filter.MatchWhole(input.Area),
                    ContactName = Filter.Split(input.ContactName),
                    TradeType = Filter.MatchWhole(input.TradeType),
                    Grain = Filter.Split(input.Grain),
                    StartTime = input.StartTime,
                    EndTime = input.EndTime
                }
            };
            var tradeProxy = ObjectFactory.TradeProxy;
            var result = tradeProxy.SearchDetail(inputPara);
            return PartialView("_TradeList", result);
        }

        [HttpPost]
        public ActionResult GetTotal(TradeFilterModel input, PageModel page)
        {
            var inputPara = new InputGetTotal
            {
                Authentication = CurrentAccount,
                PageIndex = page.pageIndex,
                PageSize = page.pageSize,
                Filter = new Data.Model.Filter.TradeTotalFilter
                {
                    EndTime = input.EndTime,
                    Grain = input.Grain,
                    StartTime = input.StartTime,
                    TradeType = input.TradeType
                }
            };
            var tradeProxy = ObjectFactory.TradeProxy;
            var result = tradeProxy.GetTotal(inputPara);
            return PartialView("Report", result);
        }

        [HttpPost]
        public ActionResult GetTotalByArea(TradeAreaFilterModel input, PageModel page)
        {
            var inputPara = new InputGetTotalByArea
            {
                Authentication = CurrentAccount,
                PageIndex = page.pageIndex,
                PageSize = page.pageSize,
                Filter = new Data.Model.Filter.TradeTotalWithAreaFilter
                {
                    Area = input.Area,
                    EndTime = input.EndTime,
                    Grain = input.Grain,
                    StartTime = input.StartTime,
                    TradeType = input.TradeType
                }
            };
            var tradeProxy = ObjectFactory.TradeProxy;
            var result = tradeProxy.GetTotalByArea(inputPara);
            return PartialView("ReportByArea", result);
        }

        [HttpPost]
        public ActionResult GetTotalByContactArea(TradeContactAreaFilterModel input, PageModel page)
        {
            var inputPara = new InputGetTotalByContact
            {
                Authentication = CurrentAccount,
                PageIndex = page.pageIndex,
                PageSize = page.pageSize,
                TradeFilter = new Data.Model.Filter.TradeTotalWithContactFilter
                {
                    Area = input.Area,
                    ContactName = input.ContactName,
                    EndTime = input.EndTime,
                    Grain = input.Grain,
                    StartTime = input.StartTime,
                    TradeType = input.TradeType
                }
            };
            var tradeProxy = ObjectFactory.TradeProxy;
            var result = tradeProxy.GetTotalByContactArea(inputPara);
            return PartialView("ReportByContactArea", result);
        }

        [HttpPost]
        public ActionResult Insert(AddTradeModel input)
        {
            if (!ModelState.IsValid)
            {
                return View(input);
            }

            var inputPara = new InputInsert
            {
                Authentication = CurrentAccount,
                Trade = new Data.Model.Table.TradeTable
                {
                    ActualMoney = input.ActualMoney,
                    ContactID = input.ContactID,
                    Grain = input.Grain,
                    Price = input.Price,
                    Remarks = input.Remarks,
                    TradeType = input.TradeType,
                    Weight = input.Weight
                }
            };
            var tradeProxy = ObjectFactory.TradeProxy;
            var result = tradeProxy.Insert(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return RedirectToAction("EditTrade", new { tradeID = result.TradeID });
        }

        [HttpPost]
        public ActionResult Update(UpdateTradeModel input)
        {
            if (!ModelState.IsValid)
            {
                return View(input);
            }
            var inputPara = new InputUpdate
            {
                Authentication = CurrentAccount,
                Trade = new Data.Model.Table.TradeTable
                {
                    TradeID = input.TradeID,
                    ActualMoney = input.ActualMoney,
                    ContactID = input.ContactID,
                    Grain = input.Grain,
                    Price = input.Price,
                    Remarks = input.Remarks,
                    TradeType = input.TradeType,
                    Weight = input.Weight
                }
            };
            var tradeProxy = ObjectFactory.TradeProxy;
            var result = tradeProxy.Update(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return RedirectToAction("EditTrade", new { tradeID = input.TradeID });
        }

        [HttpPost]
        public ActionResult Delete(int tradeID)
        {
            var inputPara = new InputDelete
            {
                Authentication = CurrentAccount,
                TradeID = tradeID
            };
            var tradeProxy = ObjectFactory.TradeProxy;
            var result = tradeProxy.Delete(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return View(Constant.RestStatusView, result);
        }
    }
}
